package com.a16.yuejian.yuejian.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.a16.yuejian.yuejian.R;
import com.a16.yuejian.yuejian.fragment.ClubClassFragment;
import com.a16.yuejian.yuejian.fragment.ClubIntroFragment;
import com.a16.yuejian.yuejian.fragment.ClubVideoFragment;
import com.a16.yuejian.yuejian.javabean.Club;
import com.a16.yuejian.yuejian.javabean.Collected;
import com.a16.yuejian.yuejian.javabean.Success;
import com.a16.yuejian.yuejian.listener.ClubListener;
import com.a16.yuejian.yuejian.listener.CollectedListener;
import com.a16.yuejian.yuejian.listener.SuccessListener;
import com.a16.yuejian.yuejian.model.ClubModel;
import com.oragee.banners.BannerView;

import java.util.ArrayList;
import java.util.List;

public class ClubActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView mclubintro,mclubvideo,mclubclass,club_title;
    private Fragment mTab01,mTab02,mTab03;
    private ImageView back,club_guanzhu;
    private Context mContext;
    private Intent intent;
    private SharedPreferences sp;
    private int[] imgs = {R.drawable.gb1,R.drawable.gb2,R.drawable.gb3,R.drawable.gb4};
    private List<View> viewList;
    BannerView bannerView;

    CollectedListener collectedListener = new CollectedListener() {
        @Override
        public void onResponse(Collected collected) {
            if(collected.getCollected().equals("1")){
                club_guanzhu.setImageResource(R.drawable.ic_aixin1);
            }else if(collected.getCollected().equals("0")){
                club_guanzhu.setImageResource(R.drawable.ic_aixin);
            }
        }

        @Override
        public void onFail() {

        }
    };

    SuccessListener followListener = new SuccessListener() {
        @Override
        public void onResponse(Success success) {
            if (success.getSuccess().equals("1")){
                Toast.makeText(ClubActivity.this,"关注成功", Toast.LENGTH_SHORT).show();
                club_guanzhu.setImageResource(R.drawable.ic_aixin1);
            }else if (success.getSuccess().equals("0")){
                Toast.makeText(ClubActivity.this,"成功取消关注", Toast.LENGTH_SHORT).show();
                club_guanzhu.setImageResource(R.drawable.ic_aixin);
            }
        }

        @Override
        public void onFail() {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_club);
        initViews();
        initEvent();
        lunbo();
        setSelect(0);
        club_title.setText(getIntent().getStringExtra("clubname"));
        ClubModel clubModel = new ClubModel();
        clubModel.isCollected(sp.getString("clubid",""),sp.getString("uid",""),"5",collectedListener);
    }

    private void setSelect(int i) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);
        switch (i){
            case 0:
                if (mTab01 == null)
                {
                    mTab01 = new ClubIntroFragment();
                    transaction.add(R.id.id_content1, mTab01);
                }else {
                    transaction.show(mTab01);
                }
                mclubintro.setTextColor(Color.parseColor("#FCCA00"));
                break;
            case 1:
                if (mTab02 == null){
                    mTab02 = new ClubClassFragment();
                    transaction.add(R.id.id_content1,mTab02);
                }else {
                    transaction.show(mTab02);
                }
                mclubclass.setTextColor(Color.parseColor("#FCCA00"));
                break;
            case 2:
                if (mTab03 == null){
                    mTab03 = new ClubVideoFragment();
                    transaction.add(R.id.id_content1,mTab03);
                }else {
                    transaction.show(mTab03);
                }
                mclubvideo.setTextColor(Color.parseColor("#FCCA00"));
                break;
            default:
                break;
        }
        transaction.commit();
    }

    private void initEvent() {
        mclubintro.setOnClickListener(this);
        mclubclass.setOnClickListener(this);
        mclubvideo.setOnClickListener(this);
        back.setOnClickListener(this);
        club_guanzhu.setOnClickListener(this);
    }

    private void initViews() {
        mclubintro = (TextView)findViewById(R.id.club_intro);
        mclubclass = (TextView)findViewById(R.id.club_class);
        mclubvideo = (TextView)findViewById(R.id.club_video);
        club_title = (TextView)findViewById(R.id.club_title);
        bannerView = (BannerView)findViewById(R.id.club_banner);
        back = (ImageView)findViewById(R.id.club_detail_back);
        club_guanzhu = (ImageView)findViewById(R.id.club_guanzhu);
        sp = getSharedPreferences("userinfo", Context.MODE_PRIVATE);
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (mTab01 !=null){
            transaction.hide(mTab01);
        }
        if (mTab02 !=null){
            transaction.hide(mTab02);
        }
        if (mTab03 !=null){
            transaction.hide(mTab03);
        }
    }

    @Override
    public void onClick(View v) {
        reset();
        switch (v.getId()){
            case R.id.club_intro:
                setSelect(0);
                break;
            case R.id.club_class:
                setSelect(1);
                break;
            case R.id.club_video:
                setSelect(2);
                break;
            case R.id.club_detail_back:
                finish();
                break;
            case R.id.club_guanzhu:
                ClubModel clubModel = new ClubModel();
                clubModel.follow(sp.getString("clubid",""),sp.getString("uid",""),"5",followListener);
                break;
            default:
                break;
        }

    }

    private void reset() {
        mclubintro.setTextColor(Color.parseColor("#C8C8C8"));
        mclubclass.setTextColor(Color.parseColor("#C8C8C8"));
        mclubvideo.setTextColor(Color.parseColor("#C8C8C8"));
    }

    private void lunbo() {
        viewList = new ArrayList<View>();
        for (int i = 0; i < imgs.length; i++) {
            ImageView image = new ImageView(ClubActivity.this);
            image.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            //设置显示格式
            image.setScaleType(ImageView.ScaleType.CENTER_CROP);
            image.setImageResource(imgs[i]);
            viewList.add(image);
        }
        bannerView.startLoop(true);
        bannerView.setViewList(viewList);
    }
}
